package com.taotao.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.common.pojo.EasyUIResult;
import com.taotao.common.service.HttpClientService;
import com.taotao.manage.pojo.Content;

@Service
public class IndexService {
	
	@Autowired
	private HttpClientService httpClientService;
	
	private static final ObjectMapper mapper = new ObjectMapper();
	
	@Value("${IMAGE_BASE_URL}")
	private String IMAGE_BASE_URL;
	@Value("${INDEX_BIG_AD}")
	private String INDEX_BIG_AD;

	public String queryBigAD() {
		
		try {
			//调用后台系统接口
			EasyUIResult<Content> easyUIResult = httpClientService
					.doGet(IMAGE_BASE_URL+INDEX_BIG_AD, new TypeReference<EasyUIResult<Content>>() {
					});

			//如果获取的数据为null,则直接返回
			if (easyUIResult==null || CollectionUtils.isEmpty(easyUIResult.getRows())) {
				return null;
			}


			//把后台提供的数据转化为前台需要的数据格式
			List<Content> list = (List<Content>) easyUIResult.getRows();

			ArrayList<Map<String, Object>> result = new ArrayList<>();
			for (Content content : list) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("srcB", content.getPic2());
				map.put("height", 240);
				map.put("alt", "");
				map.put("width", 670);
				map.put("src", content.getPic());
				map.put("href", content.getUrl());
				map.put("heightB", 240);
				result.add(map);
			}
			String jsonResult = mapper.writeValueAsString(result);
			return jsonResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
